import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.Flushable;
import java.io.IOException;
import java.net.Socket;

class transferfile extends Thread
{
	Socket ClientSoc;
	DataInputStream din;
	DataOutputStream dout;
	GUI gui;
	boolean run;
	private String Username;
	private String Password;

	transferfile(Socket soc, GUI g)
	{
		gui=g;
		Username=gui.txtuser.getText();
		Password=gui.txtpw.getText();

		try
		{
			ClientSoc=soc;
			gui.txtresults.append("connected to " + ClientSoc.getInetAddress() + "\n");
			din=new DataInputStream(ClientSoc.getInputStream());
			dout=new DataOutputStream(ClientSoc.getOutputStream());
			gui.txtresults.append("FTP Client Connected ...\n");
			run=true;
			start();
		}
		catch(Exception ex){}        
	}
	void SendFile() throws Exception
	{        
		String filename=din.readUTF();
		File f=new File(filename);
		if(!f.exists())
		{
			dout.writeUTF("File Not Found");
			return;
		}
		else
		{
			dout.writeUTF("READY");
			FileInputStream fin=new FileInputStream(f);
			int ch;
			do
			{
				ch=fin.read();
				dout.writeUTF(String.valueOf(ch));
			}
			while(ch!=-1);    
			fin.close();    
			dout.writeUTF("File Receive Successfully");                            
		}
	}

	void ReceiveFile() throws Exception
	{
		String filename=din.readUTF();
		if(filename.compareTo("File not found")==0)
		{
			return;
		}
		File f=new File(filename);
		String option;

		if(f.exists())
		{
			dout.writeUTF("File Already Exists");
			option=din.readUTF();
		}
		else
		{
			dout.writeUTF("SendFile");
			option="Y";
		}

		if(option.compareTo("Y")==0)
		{
			FileOutputStream fout=new FileOutputStream(f);
			int ch;
			String temp;
			do
			{
				temp=din.readUTF();
				ch=Integer.parseInt(temp);
				if(ch!=-1)
				{
					fout.write(ch);                    
				}
			}while(ch!=-1);
			fout.close();
			dout.writeUTF("File Send Successfully");
		}
		else {return;}
	}

	public void run()
	{
		while(run)
		{
			try
			{
				gui.txtresults.append("Waiting for Command ...\n");
				String Command=din.readUTF();
				if (Command.compareTo("LOGIN")==0)
				{
					gui.txtresults.append("\tLOGIN Command Received ... \n");
					String user = din.readUTF();
					String pass = din.readUTF();
					if (user.compareTo(Username)==0 && pass.compareTo(Password)==0){ 
						dout.writeUTF("login successful");
						gui.txtresults.append("Login successful\n");
						continue;
						}
					else { 
						dout.writeUTF("Invalid username or password");
						gui.txtresults.append("Invalid username or password\nDisconnecting...\n");
					    continue;}
				}
				else if(Command.compareTo("GET")==0)
				{
					gui.txtresults.append("\tGET Command Received ...\n");
					SendFile();
					continue;
				}
				else if(Command.compareTo("SEND")==0)
				{
					gui.txtresults.append("\tSEND Command Receiced ...\n");                
					ReceiveFile();
					continue;
				}
				else if(Command.compareTo("DISCONNECT")==0)
				{
					gui.txtresults.append("\tDisconnect Command Received ...\n");
					run=false;
					ClientSoc.close();
					Server.quit();
					new Server(gui);
				}
				run=false;
			}
			catch(Exception ex){}
		}
	}

	public void quit(){
		this.interrupt();
		try {
			dout.writeUTF("Server stopped");
			ClientSoc.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		run=false;
	}
}